package myhttp

import (
	"higame/modules/log"
	"io/ioutil"
	"net/http"
	"net/url"
)

// Get 网络请求
func Get(url string) (string, error) {
	client := http.Client{}
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		return "", err
	}
	req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3314.0 Safari/537.36 SE 2.X MetaSr 1.0")

	res, err := client.Do(req)
	if err != nil {
		return "", err
	}
	defer res.Body.Close()

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return "", err
	}

	return string(body), err
}

// Encode Url UTF-8编码
func MustEncode(sourceURL string) string {
	parseURL, err := url.Parse(sourceURL)
	if err != nil {
		log.Error.Printf("%v", err)
		return ""
	}
	return parseURL.EscapedPath()
}
